//引入api
import { getAdminCategory, getAdminAttribute, postAddattribute,postAddattributeInfo } from "@/request/api.js";
//引入element
import { Message } from 'element-ui';
//引入路由
import router from '@/router'

export default {
    namespaced: true,
    state: {
        category: [{}],
        pageSize: 10,
        count: 100,
        tableData: [{}],
        currentPage: 1,
        subsign: {
            attribute_category_id: '',
            name: "",
        }
    },
    mutations: {
        attributeList(state, payload) {
            state.tableData = payload.data
            state.count = payload.count
            state.currentPage = payload.currentPage
            state.pageSize = payload.pageSize
        },

        //修改本地vuex
        goodsGetvalue(state, payload) {
            state.subsign = payload
        },

        //获取编辑数据
        goodseditsubsign(state, payload) {
            // console.log('编辑前的参数', state.subsign)
            state.subsign = { ...payload }
            console.log('编辑后的参数', state.subsign)

        },
    },
    actions: {
        //发起请求
        goodsAddgoods({ commit, state }) {
            postAddattribute({
                ...state.subsign
            }).then(res => {
                if (res) {
                    if(state.subsign.id){
                        Message({
                            message:'修改成功',
                            type:'success'
                        })
                    }else{
                        Message({
                            message:'添加成功',
                            type:'success'
                        })
                    }
                    
                    router.push('/attribute')
                } else {
                    console.log('请求失败')
                }
            })
        },
        //获取分类数据
        attributeGetCategory({ commit, state }) {
            //初始化请求
            getAdminCategory().then(res => {
                // console.log(res.data);
                state.category = res.data;
                // console.log(state.category)
            });
        },
        //获取商品属性
        attributegetAdminAttribute({ commit, state }, payload = payload || {}) {
            getAdminAttribute({
                page: payload.currnet || state.currentPage,
                size: payload.size || state.pageSize
            }).then(res => {
                // console.log("获取商品属性", res.data)
                commit('attributeList', res.data)
            })
        },

    }
}